//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package appstore_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

const (
	CATE_LEVEL_MAJOR     = uint32(1)
	CATE_LEVEL_MINOR     = uint32(2)
	CATE_LEVEL_SUB_MINOR = uint32(3)
)

type CateDbItem struct {
	CateId       string `bson:"_id" json:"_id"`
	CateName     string `bson:"cateName" json:"cateName"`
	ParentCateId string `bson:"parentCateId" json:"parentCateId"`
	Level        uint32 `bson:"level" json:"level"`
	ChildCount   uint32 `bson:"childCount" json:"childCount"`
}

type _CateDao struct {
	mc *mongo.Collection
}

func newCateDao(db *mongo.Database) (*_CateDao, error) {
	mc := db.Collection(db_ns.DB_NS_APP_STORE + "cate")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"parentCateId", 1}, {"level", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_CateDao{mc: mc}, nil
}

func (db *_CateDao) Insert(ctx context.Context, dbItem *CateDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_CateDao) Count(ctx context.Context, parentCateId string, level uint32) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"parentCateId": parentCateId, "level": level})
	return uint32(count), err
}

func (db *_CateDao) List(ctx context.Context, parentCateId string, level uint32) ([]*CateDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"parentCateId": parentCateId, "level": level}, options.Find().SetSort(bson.M{"_id": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*CateDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_CateDao) ListAll(ctx context.Context) ([]*CateDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{})
	if err != nil {
		return nil, err
	}
	retList := []*CateDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_CateDao) ListById(ctx context.Context, cateIdList []string) ([]*CateDbItem, error) {
	if len(cateIdList) == 0 {
		return []*CateDbItem{}, nil
	}
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": cateIdList}})
	if err != nil {
		return nil, err
	}
	retList := []*CateDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_CateDao) Exist(ctx context.Context, cateId string, level uint32) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": cateId, "level": level})
	return count > 0, err
}

func (db *_CateDao) Get(ctx context.Context, cateId string, level uint32) (*CateDbItem, error) {
	retItem := &CateDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": cateId, "level": level}).Decode(retItem)
	return retItem, err
}

func (db *_CateDao) GetWithoutLevel(ctx context.Context, cateId string) (*CateDbItem, error) {
	retItem := &CateDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": cateId}).Decode(retItem)
	return retItem, err
}

func (db *_CateDao) Remove(ctx context.Context, cateId string, level uint32) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": cateId, "level": level})
	return err
}

func (db *_CateDao) UpdateName(ctx context.Context, cateId string, level uint32, cateName string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": cateId, "level": level},
		bson.M{"$set": bson.M{"cateName": cateName}})
	return err
}

func (db *_CateDao) UpdateChildCount(ctx context.Context, cateId string, level, count uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": cateId, "level": level},
		bson.M{"$set": bson.M{"childCount": count}})
	return err
}
